package com.example.leero.graduation.activity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leero.graduation.BaseActivity;
import com.example.leero.graduation.R;
import com.example.leero.graduation.Util.StepDetector;

public class RunSettingActivity extends BaseActivity {

    public static final String WEIGHT_VALUE = "weight_value";

    public static final String STEP_LENGTH_VALUE = "step_length_value";// 步长

    public static final String SENSITIVITY_VALUE = "sensitivity_value";// 灵敏值

    public static final String SETP_SHARED_PREFERENCES = "setp_shared_preferences";// 设置

    public static SharedPreferences sharedPreferences;

    private SharedPreferences.Editor editor;

    private ImageView back;

    private TextView tv_sensitivity_vlaue;
    private TextView tv_step_length_vlaue;

    private SeekBar sb_sensitivity;
    private SeekBar sb_step_length;

    private int sensitivity = 0;
    private int step_length = 0;
    private int weight = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_run_setting;
    }

    @Override
    protected void initView(Bundle state) {
        addView();
        init();
        listener();
    }

    private void addView() {
        back = (ImageView) findViewById(R.id.iv_left);
        tv_sensitivity_vlaue = (TextView) this
                .findViewById(R.id.sensitivity_value);
        tv_step_length_vlaue = (TextView) this
                .findViewById(R.id.step_lenth_value);
        sb_sensitivity = (SeekBar) this.findViewById(R.id.sensitivity);
        sb_step_length = (SeekBar) this.findViewById(R.id.step_lenth);
    }

    private void init() {
        if (sharedPreferences == null) {    //SharedPreferences是Android平台上一个轻量级的存储类，
            //主要是保存一些常用的配置比如窗口状态
            sharedPreferences = getSharedPreferences(SETP_SHARED_PREFERENCES,
                    MODE_PRIVATE);
        }

        editor = sharedPreferences.edit();

        sensitivity = 10 - sharedPreferences.getInt(SENSITIVITY_VALUE, 7);
        step_length = sharedPreferences.getInt(STEP_LENGTH_VALUE, 70);
        weight = sharedPreferences.getInt(WEIGHT_VALUE, 50);

        sb_sensitivity.setProgress(sensitivity);
        sb_step_length.setProgress((step_length - 40) / 5); //步长按钮在进度条上占得比例

        tv_sensitivity_vlaue.setText(sensitivity + "");
        tv_step_length_vlaue.setText(step_length + "厘米");
    }

    /**
     * SeekBar的拖动监听
     */
    private void listener() {
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        sb_sensitivity
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {    // 灵敏值动作的监听
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                    }
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }
                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                                                  int progress, boolean fromUser) {
                        sensitivity = progress;
                        tv_sensitivity_vlaue.setText(sensitivity + "");
                    }
                });

        sb_step_length
                .setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                    @Override
                    public void onStopTrackingTouch(SeekBar seekBar) {
                    }
                    @Override
                    public void onStartTrackingTouch(SeekBar seekBar) {
                    }
                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                                                  int progress, boolean fromUser) {
                        step_length = progress * 5 + 40;
                        tv_step_length_vlaue.setText(step_length
                                + "厘米");
                    }
                });

    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.save:
                editor.putInt(SENSITIVITY_VALUE, 10 - sensitivity);
                editor.putInt(STEP_LENGTH_VALUE, step_length);
                editor.putInt(WEIGHT_VALUE, weight);
                editor.commit();

                Toast.makeText(RunSettingActivity.this, "保存成功！", Toast.LENGTH_SHORT).show();

                this.finish();
                StepDetector.SENSITIVITY = 10 - sensitivity;
                break;

            case R.id.cancle:
                this.finish();
                break;

            default:
                break;
        }
    }
}
